﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using AttTypeDefine;

public class LoginGame : MonoBehaviour {
    GameObject m_oGameInfo;
    GameObject m_oInGame;
    GameObject m_oReBut;
    string isShowInfo;
    Toggle m_Toggle;
    bool m_bIsShowInfo;
    public string m_strGameName="游戏名称";
    public string m_strGameInfo="游戏简介";
    public EGameType m_eGameType=EGameType.Piano;
    bool m_bFirst = true;
    Button m_But;
    private void Awake()
    {
        GlobalHelper.FindGameObjectWithName(gameObject, "GameName").GetComponent<Text>().text = m_strGameName;
        m_oGameInfo = GlobalHelper.FindGameObjectWithName(gameObject, "GameInfo");
        GlobalHelper.FindGameObjectWithName(m_oGameInfo, "GameMessage").GetComponent<Text>().text = m_strGameInfo;
        m_oGameInfo.SetActive(false);
        m_Toggle = GlobalHelper.FindGameObjectWithName(m_oGameInfo, "Toggle").GetComponent<Toggle>();
        m_But = GetComponent<Button>();
        m_But.onClick.AddListener(delegate () { this.ShowInfo(); });
    }
    private void Start()
    {
        isShowInfo = "IsShow" + gameObject.name + "Info";
        if (PlayerPrefs.HasKey(isShowInfo))
        {
            if (0 == PlayerPrefs.GetInt(isShowInfo))
                m_bIsShowInfo = false;
            else
                m_bIsShowInfo = true;
        }
        else
        {
            PlayerPrefs.SetInt(isShowInfo, 1);/*1为true，显示信息；  0为false，不显示信息*/
            m_bIsShowInfo = true;
        }
    }
    void ShowInfo()
    {
        //Debug.Log(ReaderInfo.Scorelist[0].id + "  " + ReaderInfo.Scorelist[0].name + "  " + ReaderInfo.Scorelist[0].scorename + "  " + ReaderInfo.Scorelist[0].score);
        if (0 == PlayerPrefs.GetInt(isShowInfo))
            m_bIsShowInfo = false;
        else
            m_bIsShowInfo = true;
        if (m_bIsShowInfo)
        {
            m_oGameInfo.SetActive(true);
            m_But.interactable = false;
            if (m_bFirst)
            {
                m_oInGame = GlobalHelper.FindGameObjectWithName(m_oGameInfo, "YES");
                m_oInGame.GetComponent<Button>().onClick.AddListener(delegate () { this.InGame(m_eGameType); });
                m_oReBut = GlobalHelper.FindGameObjectWithName(m_oGameInfo, "NO");
                m_oReBut.GetComponent<Button>().onClick.AddListener(delegate () { this.ReGame(); });
                m_bFirst = false;
            }
        }
        else
        {
            GlobalHelper.EGameType = m_eGameType;
            UnityEngine.SceneManagement.SceneManager.LoadScene("Loading");
        }
    }
    void InGame(EGameType type)
    {
        if (m_Toggle.isOn) { }
        else
        {
            PlayerPrefs.SetInt(isShowInfo, 0);
            m_bIsShowInfo = false;
        }
        m_But.interactable = true;
        GlobalHelper.EGameType = type;
        UnityEngine.SceneManagement.SceneManager.LoadScene("Loading");
    }
    void ReGame()
    {
        if (m_Toggle.isOn) { }
        else
        {
            PlayerPrefs.SetInt(isShowInfo, 0);
            m_bIsShowInfo = false;
        }
        m_But.interactable = true;
        m_oGameInfo.SetActive(false);
    }
}
